package com.springboot.controller.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.springboot.mapper.DoctorAppointSetMapper;
import com.springboot.mapper.DoctorInfoFormMapper;
import com.springboot.model.DoctorAppointSet;
import com.springboot.service.DoctorAppointSetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.Locale;

@Controller
public class DoctorShowOrderTimeController {
    @Autowired
    DoctorAppointSetService doctorAppointSetService;
    @Autowired
    DoctorAppointSetMapper doctorAppointSetMapper;
    @Autowired
    private DoctorInfoFormMapper doctorInfoFormMapper;

    @CrossOrigin
    @RequestMapping(value = "/webgetDoctorOrderTime")
    @ResponseBody
    public PageInfo showDoctorOrderTime(@RequestParam Integer pageNum, @RequestParam Integer pageSize)
    {
        ArrayList<DoctorAppointSet> doctorsAppointSets = new ArrayList<>();
        try {
            PageHelper.startPage(pageNum,pageSize);//开启分页一点要在获取列表之前，不然不会出现分页的效果
            doctorsAppointSets = doctorAppointSetService.selectAllDoctorOrder();
            if(doctorsAppointSets!=null){
                for (DoctorAppointSet doctorsAppointSet : doctorsAppointSets) {
                    String doctorName = doctorAppointSetMapper.selectDoctorNameById(doctorsAppointSet.getDoctorId());
                    doctorsAppointSet.setDoctorName(doctorName);
                }
            }
            return PageInfo.of(doctorsAppointSets);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.out.println("DoctorShowOrderTimeController 出错");
            return PageInfo.of(new ArrayList<>());
        }
    }

    @CrossOrigin
    @GetMapping("/webgetDoctorOrderTime/{queryString}")
    @ResponseBody
    public PageInfo showDoctorOrderTime(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @PathVariable String queryString){
        boolean status = queryString.contains("-");
        ArrayList<DoctorAppointSet> doctorsAppointSets = new ArrayList<>();
        if(status){  //查询内容是日期
            try {
                PageHelper.startPage(pageNum,pageSize);
                doctorsAppointSets = doctorAppointSetMapper.selectByBookDate(queryString);
                if(doctorsAppointSets!=null){
                    for (DoctorAppointSet doctorsAppointSet : doctorsAppointSets) {
                        String doctorName = doctorAppointSetMapper.selectDoctorNameById(doctorsAppointSet.getDoctorId());
                        doctorsAppointSet.setDoctorName(doctorName);
                    }
                }
                return PageInfo.of(doctorsAppointSets);
            }catch (Exception e){
                e.printStackTrace();
                return PageInfo.of(doctorsAppointSets);
            }
        }else {  //查询内容是医生名
            try {
                int doctorId = doctorAppointSetMapper.selectDoctorIdByName(queryString);
                PageHelper.startPage(pageNum,pageSize);
                doctorsAppointSets = doctorAppointSetMapper.selectByDoctorId(doctorId);
                if(doctorsAppointSets!=null){
                    for (DoctorAppointSet doctorsAppointSet : doctorsAppointSets) {
                        doctorsAppointSet.setDoctorName(queryString);
                    }
                }
                return PageInfo.of(doctorsAppointSets);
            }catch (Exception e){
                e.printStackTrace();
                return PageInfo.of(doctorsAppointSets);
            }
        }
    }
}
